import { post } from 'server/axios.js'
import { config_list_column_post } from 'server/modules/1890.js'

// 接口拉取服务端数据
export const executeServer = async function (state) {
  const { interfaceURL, localApiParams, query, hasPaginate } = state
  if (!state.interfaceURL) {
    return false
  }

  state.spinning = true

  let paginate = {}
  if (hasPaginate) {
    paginate = {
      pageNum: state.pageNum,
      pageSize: state.pageSize
    }
  }
  const serveParams = {
    ...paginate,
    ...localApiParams,
    ...query
  }
  if (state.callback) {
    state.callback(serveParams)
  }
  const { success, data } = await post(
    `${process.env.VUE_APP_CONFIG}/${interfaceURL}`,
    serveParams
  )
  if (success) {
    if (Object.prototype.toString.call(data) === '[object Object]') {
      state.tableDatas = data.list || []
    } else {
      state.tableDatas = data || []
    }
    state.total = data?.total || 0
    if (state.afterRespLoad) {
      state.afterRespLoad(data.list)
    }
    // pageInfo.pageSize = data.pageSize
  } else {
    state.tableDatas = []
  }
  state.spinning = false
}
/**
 * 拉取每个列的筛选数据
 * @param {*} item
 */
export const getConfigColumn = async function (item, state) {
  const result = await config_list_column_post(item.params)
  state.$set(item, 'plainOptions', result.data || [])
  state.options = result.data
  state.loading = false
  state.$forceUpdate()
}
/**
 * 获取用户选择的条件参数
 * @param {*} state
 */
export const getFilterParams = function (state, isBacth) {
  const query = { sortList: [] }
  state.tableColumns.forEach(_ => {
    if (_.desc) {
      query.sortList.push({
        desc: _.desc,
        field: _.dataIndex
      })
    }
    if (_.isBatch) {
      query[_.key] = _.batchTags
    } else {
      if (_.needEmpty === 1) {
        if (_.isBlank) {
          const index = _.filterValue.findIndex(item => item === '')
          index === -1 && _.filterValue.push('')
        } else {
          _.filterValue = _.filterValue.filter(v => v !== '')
        }
      }
      if (_.filterValue && _.filterValue.length) {
        query[_.key] = _.filterValue
      }
    }
  })
  state.query = {
    ...query,
    ...state.queryParams
  }
}
export const executeSearch = function (state, isBatch) {
  getFilterParams(state, isBatch)
  executeServer(state, isBatch)
}

/**
 * 根据列标题获取列宽
 * @param {*} column
 * @returns
 */
export const useColumnWidth = (column, state) => {
  const { title: columnName, width, slots, dataIndex } = column
  const columnWidth = state.columnWidth || {}
  if (width) {
    return width
  }
  if (columnWidth[dataIndex]) {
    return columnWidth[dataIndex]
  }

  let fixedWidth = 0
  for (let i = 0; i < columnName.length; i++) {
    const charCode = columnName.charCodeAt(i)
    if (charCode >= 0 && charCode <= 128) {
      fixedWidth += 8
    } else {
      fixedWidth += 16
    }
  }
  // 添加两边的间距 5是误差
  fixedWidth += 16 + 5
  //  有筛选
  if (slots?.filterDropdown === 1) {
    fixedWidth += 28
  }
  if (fixedWidth < 160) {
    return 160
  }
  if (fixedWidth > 320) {
    return 320
  }
  return fixedWidth
}
